const validate = require('../../../../../helper/validate.js');
const ProjectBiz = require('../../../biz/project_biz.js');
const projectSetting = require('../../../public/project_setting.js');
const setting = require('../../../../../setting/setting.js');
const PassportBiz = require('../../../../../comm/biz/passport_biz.js');
const RequestBiz = require('../../../../../comm/biz/request_biz.js');

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		isLoad: false,
		isEdit: false,
		openid: null,
		mobileCheck: setting.MOBILE_CHECK,
		showPrivacy: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
		ProjectBiz.initPage(this);

		if (options && options.retUrl)
			this.data.retUrl = decodeURIComponent(options.retUrl);

		await this._loadDetail();
	},

	_loadDetail: async function (e) {
		let _self = this;
		wx.login({
			success (res) {
				if (res.code) {
					let req = RequestBiz.request('/api/ysjl/user/wxAuth', {code: res.code});
					req.then(res => {
						if (res.data.ret == 0) {
							let retData = res.data.data;
							if (retData.id) {
								PassportBiz.setToken(retData);
								wx.reLaunch({ url: '../index/my_index' });
							}
							else {
								_self.setData({
									isLoad: true,
									openid: retData.openid
								})
							}
						}
					});
				}
			}
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: async function () {
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	bindPicTap: function (e) {  
		this.setData({
			formPic: e.detail.avatarUrl
		})
	},

	bindSubmitTap: async function (e) {
		let data = this.data;

		// 数据校验 
		data = validate.check(data, projectSetting.USER_CHECK_FORM, this);
		if (!data) return;

		let forms = this.selectComponent("#cmpt-form").getForms(true);
		if (!forms) return;
		data.forms = forms;

		wx.showLoading({ title: '头像上传中' });
		let picReq = await RequestBiz.uploadFile('/api/ysjl/user/uploadPic', data.pic, {});
		wx.hideLoading();
		let req = RequestBiz.request('/api/ysjl/user/register', {name: data.name, pic: JSON.parse(picReq.data).data, openid: this.data.openid});
		req.then(res => {
			let retData = res.data;
			if (retData.ret == 0) {
				PassportBiz.setToken(retData.data);
				wx.reLaunch({ url: '../index/my_index' });
			}
		})
	},

	
	agree(e){
		console.log("用户同意隐私授权, 接下来可以调用隐私协议中声明的隐私接口")
	},
	disagree(e){
		console.log("用户拒绝隐私授权, 未同意过的隐私协议中的接口将不能调用")
	},
})